package com.dkts.oil.converter;

import com.dkts.oil.config.OilConfig;
import com.dkts.oil.enumes.WriterTypeEnums;
import com.dkts.oil.mysql.entity.OilObstacle;
import com.dkts.oil.strtree.GeoJsonSTRtreeIndex;
import com.dkts.oil.web.vo.request.OilStorageTankRequest;
import com.dkts.oil.web.vo.request.PipelineRouteRequest;
import com.dkts.oil.web.vo.request.SiteSelectionRequest;
import com.dkts.oil.web.vo.request.subReq.ObstacleType;
import com.dkts.oil.web.vo.request.subReq.SqlWhere;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.extern.slf4j.Slf4j;
import org.locationtech.jts.geom.Geometry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Schema(description = "油库选址转换类V2")
@Slf4j
@Service
public class OilAddressV2Converter {

    @Autowired
    private OilConfig oilConfig;

    @Autowired
    private  GeoJsonSTRtreeIndex geoJsonSTRtreeIndex;

    public List<Geometry> listObstacles(Geometry planGeometry2, SiteSelectionRequest request,boolean isSkipCustom) throws Exception {
        List<OilObstacle> data= geoJsonSTRtreeIndex.queryByPolygon(planGeometry2,GeoJsonSTRtreeIndex.strTreeBuild);
        List<Geometry> listObstacles=new ArrayList<>();
        String url=oilConfig.getTempDir();
        if(data!=null) {
            log.info("障碍物数据：{}", data.size());
            SiteSelectionRequest.ConstraintConfig constraintConfig= request.constraintConfig;

            for (int i=0;i<data.size();i++){
                OilObstacle oilObstacle = data.get(i);
                Geometry geometry = oilObstacle.getGeometry();//GeometryConverter.convert(oilObstacle.getGeoData());
                if (! planGeometry2.intersects( geometry)){
                    continue;
                }

                //水库最小距离（单位：米） 41 河渠 42 湖泊 43 :水库坑塘
                //河岸 -2
                Integer label= oilObstacle.getLabel();
                if(label ==-2 ){
                    Geometry de=GeometryConverter.getBuff(geometry,constraintConfig.getMinDistanceToLake());
                    WriterToFile.writerFileGeoJson(url+"距离湖泊-"+constraintConfig.getMinDistanceToLake()+"-"+i+".geojson",geometry);
                    listObstacles.add(de);
                    continue;
                }

                //水库 -3
                if(label == -3 ){
                    Geometry de=GeometryConverter.getBuff(geometry,constraintConfig.getMinDistanceToReservoir());
                    WriterToFile.writerFileGeoJson(url+"距离水库-"+constraintConfig.getMinDistanceToReservoir()+"-"+i+".geojson",geometry);
                    listObstacles.add(de);
                    continue;
                }

                //城乡、工矿、居民用地 建筑物
                if(label == -1 ){
                    Geometry de=GeometryConverter.getBuff(geometry,constraintConfig.getMinDistanceToResidential());
                    WriterToFile.writerFileGeoJson(url+"距离居民区域-"+constraintConfig.getMinDistanceToResidential()+"-"+i+".geojson",geometry);
                    listObstacles.add(de);
                    continue;
                }
                listObstacles.add(geometry);
            }
        }
        if(request.getCustomArea() !=null && !isSkipCustom){
            log.info("手动设置障碍物：{}", request.getCustomArea().size());
            List<SiteSelectionRequest.CustomAreaRequest> d=request.getCustomArea();
            for (int i=0; i < d.size(); i++){
                SiteSelectionRequest.CustomAreaRequest obstacleType = d.get(i);
                Geometry geometry2= GeometryConverter.getBuff(obstacleType.getCustomAreaPolygon(), obstacleType.getDistance());
                WriterToFile.writerFileGeoJson(url+"自定义绘制区域-"+obstacleType.getDistance()+"-"+i+".geojson",geometry2);
                listObstacles.add(geometry2);
            }
        }
        return listObstacles;
    }

    public List<Geometry> routeObstacles(Geometry planGeometry2, PipelineRouteRequest request, boolean isSkipCustom) throws Exception {
        List<OilObstacle> data= geoJsonSTRtreeIndex.queryByPolygon(planGeometry2,GeoJsonSTRtreeIndex.strTreeBuild);
        List<Geometry> listObstacles=new ArrayList<>();
        String url=oilConfig.getTempDir();
        if(data!=null) {
            log.info("routeObstacles 障碍物数据：{}", data.size());
            PipelineRouteRequest.ConstraintConfig constraintConfig= request.constraintConfig;

            for (int i=0;i<data.size();i++){
                OilObstacle oilObstacle = data.get(i);
                Geometry geometry = oilObstacle.getGeometry();//GeometryConverter.convert(oilObstacle.getGeoData());
                if (! planGeometry2.intersects( geometry)){
                    continue;
                }

                //水库最小距离（单位：米） 41 河渠 42 湖泊 43 :水库坑塘
                //河岸 -2
                Integer label= oilObstacle.getLabel();
                if(label ==-2 ){
                    Geometry de=GeometryConverter.getBuff(geometry,constraintConfig.getMinDistanceToLake());
                    WriterToFile.writerFileGeoJson(url+"距离湖泊-"+constraintConfig.getMinDistanceToLake()+"-"+i+".geojson",geometry);
                    listObstacles.add(de);
                    continue;
                }

                //水库 -3
                if(label == -3 ){
                    Geometry de=GeometryConverter.getBuff(geometry,constraintConfig.getMinDistanceToReservoir());
                    WriterToFile.writerFileGeoJson(url+"距离水库-"+constraintConfig.getMinDistanceToReservoir()+"-"+i+".geojson",geometry);
                    listObstacles.add(de);
                    continue;
                }

                //城乡、工矿、居民用地 建筑物
                if(label == -1 ){
                    Geometry de=GeometryConverter.getBuff(geometry,constraintConfig.getMinDistanceToResidential());
                    WriterToFile.writerFileGeoJson(url+"距离居民区域-"+constraintConfig.getMinDistanceToResidential()+"-"+i+".geojson",geometry);
                    listObstacles.add(de);
                    continue;
                }
                listObstacles.add(geometry);
            }
        }
        if(request.getCustomArea() !=null && !isSkipCustom){
            log.info("routeObstacles-手动设置障碍物：{}", request.getCustomArea().size());
            List<PipelineRouteRequest.CustomAreaRequest> d=request.getCustomArea();
            for (int i=0; i < d.size(); i++){
                PipelineRouteRequest.CustomAreaRequest obstacleType = d.get(i);
                Geometry geometry2= GeometryConverter.getBuff(obstacleType.getCustomAreaPolygon(), obstacleType.getDistance());
                WriterToFile.writerFileGeoJson(url+"routeObstacles-自定义绘制区域-"+obstacleType.getDistance()+"-"+i+".geojson",geometry2);
                listObstacles.add(geometry2);
            }
        }
        return listObstacles;
    }
}
